package com.by.toolkitspring.logback;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.pattern.CompositeConverter;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.by.toolkit.consts.DatePatternConst;

import java.time.LocalDateTime;
import java.time.ZoneOffset;

public class BusinesslogReplaceConverter extends CompositeConverter<ILoggingEvent> {

    public static final String PROJECT_NAME_VAL = "businesslog";

    @Override
    protected String transform(ILoggingEvent iLoggingEvent, String s) {
        JSONObject jsonObject = JSON.parseObject(s);
        jsonObject.put("@timestamp", LocalDateTimeUtil.format(LocalDateTime.now(ZoneOffset.UTC), DatePatternConst.LOG_TIME_PATTERN));
        jsonObject.put("project", PROJECT_NAME_VAL);
        return jsonObject.toJSONString() + "\n";
    }

}
